﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace RBAC.Web.CustomHelpers
{
    public class ExternalLookupHtmlBuilder<TModel, TProperty>
    {
        private HtmlHelper<TModel> _helper;
        private Expression<Func<TModel, int>> _lookupId;
        private Expression<Func<TModel, TProperty>> _lookupCode;        
        private string _classname;

        public ExternalLookupHtmlBuilder(HtmlHelper<TModel> helper, String className)
        {
            _helper = helper;
            _classname = className;
        }

		public ExternalLookupHtmlBuilder(HtmlHelper<TModel> helper,Expression<Func<TModel, TProperty>> property, String className)
        {
            _helper = helper;
            _classname = className;
            _lookupCode = property;
        }

        public ExternalLookupHtmlBuilder<TModel, TProperty> For(Expression<Func<TModel, int>> property)
        {
            _lookupId = property;
            return this;
        }

        public ExternalLookupHtmlBuilder<TModel, TProperty> WithCode(Expression<Func<TModel, TProperty>> property)
        {
            _lookupCode = property;
            return this;
        }

        public MvcHtmlString Render()
        {
            return Render(false);
        }

        public MvcHtmlString Render(bool disable)
        {
            StringBuilder sb = new StringBuilder();
            var urlHelper = new UrlHelper(_helper.ViewContext.RequestContext);

            string ButtonName = _classname + "ShowAll";
            string SearchName = _classname + "LookupField";            
            if (disable)
            {
                 //old code
				//sb.Append(_helper.TextBoxFor(_lookupCode, new { id = SearchName, @class = "txtFillCodeExternal", @readonly = "readonly" }));
                //sb.Append(String.Format("<input type=\"button\" id=\"{0}\" value=\"...\" class=\"btnLookup\" disabled/>", ButtonName));
                sb.Append(String.Format("<div class=\"col-md-12 form-group\">"));
                sb.Append(String.Format("<div class=\"input-group date\" >"));
                sb.Append(_helper.TextBoxFor(_lookupCode, new { id = SearchName, @class = "form-control", @readonly = "readonly" }));
                sb.Append(String.Format("<span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-search\" disabled></span></span>"));
                sb.Append(String.Format("</div>"));
                sb.Append(String.Format("</div>"));
			}
            else
            {   //old code
                //sb.Append(_helper.TextBoxFor(_lookupCode, new { id = SearchName, @class = "txtFillCodeExternal" }));
                //sb.Append(String.Format("<input type=\"button\" id=\"{0}\" value=\"...\" class=\"btnLookup\" />", ButtonName));
                 sb.Append(String.Format("<div class=\"col-md-12 form-group\">"));
                sb.Append(String.Format("<div class=\"input-group date\" id=\"{0}\">", ButtonName));
                sb.Append(_helper.TextBoxFor(_lookupCode, new { id = SearchName, @class = "form-control" })); 
                sb.Append(String.Format("<span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-search\"></span></span>"));
                sb.Append(String.Format("</div>"));
                sb.Append(String.Format("</div>"));
			}

            sb.Append(String.Format("<input type=\"hidden\" id=\"{0}GetExternalLookup\" value=\"{1}\" />", _classname, urlHelper.Action(_classname + "GetExternalLookup", "Popup")));
            sb.Append(String.Format("<input type=\"hidden\" id=\"{0}PopExternalLookup\" value=\"{1}\" />", _classname, urlHelper.Action(_classname + "PopExternalLookup", "Popup")));

            sb.Append(_helper.HiddenFor(_lookupId, new { id = _classname, @class = "TextLookup" }));

            return new MvcHtmlString(sb.ToString());
        }

        public override string ToString()
        {
            return Render().ToString();
        }
    }
}